#coding=utf-8

import argparse
from datetime import datetime

def dataParserFile(fName, start_date, end_date):

    print('ready open file: ', fName, start_date, end_date)

    dateformater = '%Y/%m/%d %H:%M:%S'

    fp = open(fName, 'r')

    while True:
        line = fp.readline()
        if line == '':
            print('file End...')
            break;

        if line.find('appRpcHandler') > 0:

            if start_date is not None:
                s_date = datetime.strptime(start_date, dateformater)
                datestr = line[5:5+19]
                l_date = datetime.strptime(datestr, dateformater)
                # print('s_date: ' , (s_date))
                # print('l_date: ' , (l_date))
                if l_date < s_date:
                    continue

            if end_date is not None:
                s_date = datetime.strptime(end_date, dateformater)
                datestr = line[5:5+19]
                l_date = datetime.strptime(datestr, dateformater)
                if l_date > s_date:
                    continue

            print('find: ', line)
            index = line.find('RX: ')
            substr = line[index+4:]
            print('get: ' + substr)
            str = '00 00 00 00 00'
            strlen = len(substr)

            for i in range(0, strlen, 2):
                str = str + ' ' + substr[i:i+2]

            dataParser(str)
            # break;

    fp.close()


def dataParser(data):
    print('data: ', data)

    values = data.split(' ')

    cfg = [
        {'title': 'Identifier', 'index': [0, 1], 'des': '协议头'},
        {'title': 'Length', 'index': [2, 3], 'des': 'AID 到 Tail的所有数据长度'},
        {'title': 'AID', 'index': [4], 'des': ''},
        {'title': 'MID', 'index': [5], 'des': ''},
        {'title': 'App Data Length', 'index': [6, 7], 'des': ''},
        {'title': 'remote_ctrl', 'index': [8], 'des': '0x0: DISARMED, 0x01: ARMED, 0x02: ALARM, 0x03: HALF_ARMED'},
        {'title': 'motor_layout', 'index': [9], 'des': '0x0: DISARMED, 0x01: ARMED, 0x02: ALARM, 0x03: HALF_ARMED'},
        {'title': 'bcm_antiTheftSts(防盗状态)', 'index': [10], 'des': '0x0: DISARMED, 0x01: ARMED, 0x02: ALARM, 0x03: HALF_ARMED'},
        {'title': 'bcm_remoteMode(远程模式)', 'index': [11], 'des': ' 0x0: INVALID, 0x1: NORMAL, 0x2: REMOTE'},
        {'title': 'BCM_ModeFB(情景模式)', 'index': [12], 'des': ' 0x0: Reserved, 0x1: OFF, 0x2: ON, 0x3: INVALID'},
        {'title': 'esc_vehicleSpeed(车速)', 'index': [13,16], 'des': '精度：0.05625Km/h'},
        {'title': 'esc_vehicleSpeedValid(车速有效性)', 'index': [17], 'des': '0x0: INVALID, 0x1: VALID'},
        {'title': 'vcu_actualGear(挡位)', 'index': [18], 'des': '0x0: P, 0x1: R, 0x2: N, 0x3: D'},
        {'title': 'vcu_actualGearValid(挡位有效性)', 'index': [19], 'des': '0x0: INVALID, 0x1: VALID'},
        {'title': 'esc_epbStatus(EPB系统状态)', 'index': [20], 'des': ' 0x0: DEFAULT, 0x1: PARKED, 0x2: RELEASED, 0x3: PARKING, 0x4: RELEASING, 0x5: RESERVED_EPB12, 0x6: RESERVED_EPB13, 0x7: RESERVED_EPB14'},
        {'title': 'bms_SOC(动力电池电量)', 'index': [21, 22], 'des': '精度：0.1%'},
        {'title': 'bms_acChargeConnection(交流充电连接状态)', 'index': [23], 'des': '0x0: DISCONNECT, 0x1: CONNECT'},
        {'title': 'bms_dcChargeConnection(直流充电连接状态)', 'index': [24], 'des': ' --- 0x0: DISCONNECT, 0x1: CONNECT'},
        {'title': 'obc_state(OBC系统状态)', 'index': [25], 'des': '0x0: INIT, 0x1: STANDBY, 0x2: AC_CHARGE_MODE, 0x3: DC_CHARGE_MODE, 0x4: RESERVED1, 0x5: RESERVED1, 0x6: RESERVED1, 0x7: RESERVED1, 0x8: RESERVED1, 0x9: FAULT, 0xA: SLEEP, 0xB: V2X, 0xC: BATT_WARM 电池加热, 0xD: RESERVED1, 0xE: RESERVED2, 0xF: RESERVED3'},
        {'title': 'obc_CcResistance(CC电阻值)', 'index': [26], 'des': '0x0: invalid 无效, 0x1: UNCONNECT 未连接, 0x2: HALFCONNECT 半连接, 0x3: 1500 Ohm, 0x4: 680 Ohm, 0x5: 220 Ohm, 0x6: 100 Ohm, 0x7: RESERVED, 0x8: RESERVED, 0x9: 2000 Ohm, 0xA:RESERVED, 0xB:RESERVED, 0xC:RESERVED, 0xD:RESERVED, 0xE:RESERVED, 0xF:RESERVED'},
        {'title': 'vcu_ExhibitionMode(展车模式状态)', 'index': [27], 'des': '0x0: OFF, 0x1: ON, 0x2: INVALID, 0x3: Reserved'},
        {'title': 'GW_OBD_Diag(OBD 接口有设备连接)', 'index': [28], 'des': '0x0:NO DIAG, 0x1: DIAGING'},
        {'title': 'bcm_pepsPowerMode(电源模式)', 'index': [29], 'des': '0x0: DEFAULT_POWERMODE, 0x1: OFF_POWERMODE, 0x2: ON_POWERMODE, 0x3: RESERVE_POWERMODE, 0x4: RESERVE_POWERMODE1, 0x5: RESERVE_POWERMODE2, 0x6: RESERVE_POWERMODE3, 0x7: INVALID_POWERMODE'},
        {'title': 'BCM_PowerState(BCM电源状态机)', 'index': [30], 'des': ''},
        {'title': 'BCM_PresentONState(Present ON 状态机)', 'index': [31], 'des': ''},
        {'title': 'vcu_hvState(高压状态)', 'index': [32], 'des': '0x0: HIGH_VOLT_DOWN 高压下电状态, 0x1: HIGH_VOLT_UP 高压状态, 0x2: HIGH_VOLT_DOWNING 正在下高压, 0x3: HIGH_VOLT_UPING 正在上高压'},
        {'title': 'bms_acChargeState(交流充电状态)', 'index': [33], 'des': '0x0: OFF, 0x1: INIT, 0x2: STANDBY, 0x3: CHARGING, 0x4: CHARGE_END, 0x5: Warming, 0x6: FAULT'},
        {'title': 'bms_dcChargeState(直流充电状态)', 'index': [34], 'des': '0x0: OFF, 0x1: INIT, 0x2: STANDBY, 0x3: CHARGING, 0x4: CHARGE_END, 0x5: Warming, 0x6: FAULT'},
        {'title': 'bcm_FuelTapStatus(加油口盖状态)', 'index': [35], 'des': '0x0: CLOSED, 0x1: OPEN, 0x2: TAPERROR, 0x3: None'},
        {'title': 'BCM_ebs_SOC(蓄电池SOC百分比)', 'index': [36, 37], 'des': '精度 1'},
        {'title': 'BCM_ebs_SOC_STATE(EBP学习状态)', 'index': [38], 'des': '0x0: deviation > 15%, 0x1: deviation <15%, 0x2: deviation < 10%, 0x3: invalid'},
        {'title': 'bms_v2vChrgConnection(V2V充电枪连接状态)', 'index': [39], 'des': '0x0: Disconnect, 0x1: Connected'},
        {'title': 'vcu_ready(Powertrain ready)', 'index': [40], 'des': '0x0: NOT_READY, 0x1: READY'},
        {'title': 'BCM_ebs_U_BATT(蓄电池电压)', 'index': [41, 44], 'des': ''},
        {'title': 'acu_driverSeatBeltStatus(安全带状态)', 'index': [45], 'des': ''},
        {'title': 'bcm_doorFLSts(驾驶室车门状态)', 'index': [46], 'des': ''}
           ]

    for c in cfg:
        title = c['title']
        index = c['index']
        des = c['des']
        if len(index) > 1:
            i0 = index[0]
            i1 = index[1]
            l0 = list(range(i0, i1+1))
            l0.reverse()
            vs = ''
            for v in l0:
                vs += values[v]

            if len(des) > 0:
                print(title + ': 0x' + vs + '=' + str(int(vs, 16)) + ' --- ' + des)
            else:
                print(title + ': 0x' + vs + '=' + str(int(vs, 16)))
        else:
            if len(des) > 0:
                print(title + ': 0x' + values[index[0]] + ' --- ' + des)
            else:
                print(title + ': 0x' + values[index[0]])


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--data', type=str, default=None)
    parser.add_argument('--f', type=str, default=None)
    parser.add_argument('--start_date', type=str, default=None)
    parser.add_argument('--end_date', type=str, default=None)
    args = parser.parse_args()

    data = args.data
    fName = args.f

    if fName is None:
        if data is not None:
            dataParser(data)
        else:
            print('data:', data)
    else:
        dataParserFile(fName, args.start_date, args.end_date)
